import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/page/home/model/user_info_entity.dart';
import 'package:tender_flutter/page/person/api/api_person.dart';
import 'package:tender_flutter/providers/provider.dart';
import 'package:tender_flutter/providers/user_profile.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/toast.dart';
import 'package:tender_flutter/utils/utils.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';

class InviateBindingPage extends StatefulWidget {
  @override
  _InviateBindingPageState createState() => _InviateBindingPageState();
}

class _InviateBindingPageState extends State<InviateBindingPage> {
  String inviteStr;

  /// 获取用户信息
  Future _getUserInfo() async {
    var response = await ApiPerson.getUserInfo<UserInfoEntity>();
    Store.value<UserProfileInfo>(context).setUserInfo(response.result);
  }

  /// 绑定邀请码
  _bindingInvite() async {
    if (isEmpty(inviteStr)) {
      ToastUtils.show("请输入邀请码后绑定");
      return;
    }
    ToastUtils.showLoading("绑定中.");
    var response = await ApiPerson.bindingInvite(invite: inviteStr);
    ToastUtils.dismissLoading();
    if (response.status == Status.SUCCESS) {
      LRouter.navigatorCtx(context).pop();
      ToastUtils.show("邀请码绑定成功");
    } else {
      ToastUtils.show("邀请码绑定失败");
    }
  }

  @override
  void initState() {
    super.initState();

    /// 页面渲染完成之后的回调，只执行一次
    WidgetsBinding.instance.addPostFrameCallback((mag) {
      _getUserInfo();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ThemeColors.colorWhite,
        // appBar: _initAppBar(),
        body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
              //设置Container修饰
              image: DecorationImage(
                //背景图片修饰
                image: AssetImage(RPath.imgPath + "binding_invite_bg.png"),
                fit: BoxFit.cover, //覆盖
              ),
            ),
            child: Column(children: [
              _initAppBar(),
              SizedBox(height: ScreenAdapter.height(80)),
              Expanded(
                  child: SingleChildScrollView(
                      physics: BouncingScrollPhysics(), child: _initBody()))
            ])));
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("绑定邀请码",
          style: FonStyle.navTextStyle(textColor: ThemeColors.colorWhite)),
      backgroundColor: ThemeColors.colorTransparent,
      backColor: ThemeColors.colorWhite,
      brightness: Brightness.dark,
      elevation: 0,
    );
  }

  Widget _initBody() {
    return Consumer(
        builder: (BuildContext ctx, UserProfileInfo profileInfo, Widget child) {
      inviteStr = profileInfo.userInfo.inviteCode;
      return Center(
          child: AspectRatio(
              aspectRatio: 712 / 933,
              child: Stack(
                fit: StackFit.expand,
                children: [
                  Image.asset(
                    RPath.imgPath + "invite_card_bg.png",
                    fit: BoxFit.fill,
                  ),
                  Column(children: [
                    SizedBox(height: ScreenAdapter.height(130)),
                    Image.asset(
                      RPath.imgPath + "invite_bind_title.png",
                      fit: BoxFit.cover,
                    ),
                    SizedBox(height: ScreenAdapter.height(80)),
                    Container(
                      margin: EdgeInsets.only(
                          left: ScreenAdapter.width(70),
                          right: ScreenAdapter.width(70)),
                      height: ScreenAdapter.height(100),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        //设置Container修饰
                        image: DecorationImage(
                          //背景图片修饰
                          image:
                              AssetImage(RPath.imgPath + "invite_input_bg.png"),
                          fit: BoxFit.fill, //覆盖
                        ),
                      ),
                      child: TextField(
                        decoration: InputDecoration(
                          hintText: "请输入好友给你分享的邀请码",
                          focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          disabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          border: OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          contentPadding: EdgeInsets.symmetric(vertical: 0),
                        ),
                        keyboardType: TextInputType.number,
                        style: TextStyle(
                            color: ThemeColors.colorType4,
                            fontSize: ScreenAdapter.sp(34),
                            fontWeight: FontWeight.bold),
                        // focusNode: focusNode,
                        textAlign: TextAlign.center,
                        controller: TextEditingController(text: inviteStr),
                        readOnly: isEmpty(profileInfo.userInfo.inviteCode)
                            ? false
                            : true,
                        maxLines: 1,
                        autocorrect: true,
                        autofocus: false,
                        onChanged: (value) {
                          inviteStr = value;
                        },
                      ),
                    ),
                    SizedBox(height: ScreenAdapter.height(120)),
                    Container(
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.only(left: ScreenAdapter.width(70)),
                        child: Text(
                          "温馨提示",
                          style: TextStyle(
                              color: ThemeColors.colorType24,
                              fontSize: ScreenAdapter.sp(28),
                              fontWeight: FontWeight.bold),
                        )),
                    Container(
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.only(
                            left: ScreenAdapter.width(70),
                            top: ScreenAdapter.height(30)),
                        child: Text(
                          """① 成功邀请好友下载权鸟夺标 APP\n② 下载注册成功后开通会员\n③关联邀请码，即可获得现金奖励哦!""",
                          style: TextStyle(
                              color: ThemeColors.colorType24,
                              fontSize: ScreenAdapter.sp(26),
                              fontWeight: FontWeight.w500),
                        )),
                    Opacity(
                        opacity:
                            isEmpty(profileInfo.userInfo.inviteCode) ? 1 : 0.3,
                        child: AbsorbPointer(
                            absorbing: isEmpty(profileInfo.userInfo.inviteCode) ? false : true,
                            child: Container(
                              margin: EdgeInsets.only(
                                  left: ScreenAdapter.width(80),
                                  right: ScreenAdapter.width(80),
                                  top: ScreenAdapter.height(40)),
                              width: double.infinity,
                              height: ScreenAdapter.height(90),
                              child: FlatButton(
                                color: ThemeColors.colorType10,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(
                                      ScreenAdapter.width(14)),
                                ),
                                child: Text(
                                  isEmpty(profileInfo.userInfo.inviteCode) ? "绑定" : "已绑定",
                                  style: TextStyle(
                                      color: ThemeColors.colorWhite,
                                      fontSize: ScreenAdapter.sp(30)),
                                ),
                                onPressed: () {
                                  _bindingInvite();
                                },
                              ),
                            )))
                  ]),
                ],
              )));
    });
  }
}
